package ace.module.account.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.core.utils.SystemClockUtils;
import ace.module.account.api.model.config.IamAccountBaseConfig;
import ace.module.account.api.model.config.IamApplicationCaptchaSmsAppConfig;
import ace.module.account.api.model.dto.IamApplicationConfigDto;
import ace.module.account.core.impl.dao.entity.IamApplicationConfig;
import ace.module.account.core.impl.manager.IamApplicationConfigManager;
import ace.module.account.core.impl.manager.biz.IamApplicationConfigBiz;
import ace.module.account.core.impl.manager.cache.IamApplicationConfigCacheManager;
import ace.module.common.core.impl.manager.impl.AbstractEntityOfAppManager;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/3/20 9:28
 */
@AllArgsConstructor
@Component
public class IamApplicationConfigManagerImpl
    extends AbstractEntityOfAppManager<IamApplicationConfig, Long, IamApplicationConfigDto>
    implements IamApplicationConfigManager {


  private final IamApplicationConfigCacheManager iamApplicationConfigCacheManager;
  private final IamApplicationConfigBiz iamApplicationConfigBiz;

  @Override
  public IamAccountBaseConfig getBaseConfig(Long iamAppId) {
    return iamApplicationConfigCacheManager.getBaseConfig(iamAppId);
  }

  @Override
  public IamApplicationCaptchaSmsAppConfig getCaptchaSmsConfig(Long iamAppId) {
    return iamApplicationConfigCacheManager.getCaptchaSmsConfig(iamAppId);
  }

  @Override
  public IamApplicationConfig.IamApplicationConfigBuilder createBuilder(Object config) {
    return IamApplicationConfig.builder()
        .id(this.getNewId())
        .config(iamApplicationConfigBiz.convertToJson(config))

        .createTime(SystemClockUtils.currentTimeMillis())
        .updateTime(SystemClockUtils.currentTimeMillis())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .deleteFlag(CoreConstant.DELETE_FLAG_NOT);
  }

}
